﻿using Ellanet.Annotations;
using Ellanet.Classes;
using Ellanet.Converters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Timers;

namespace Ellanet.ViewModels
{
    class ResultsWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        //private string _movieGifUrl;
        private Statistic _currentStatistic;

        //public string MovieGifUrl
        //{
        //    get { return _movieGifUrl; }
        //    set
        //    {
        //        _movieGifUrl = value;
        //        OnPropertyChanged();
        //    }
        //}

        public Statistic CurrentStatistic
        {
            get { return _currentStatistic; }
            set
            {
                _currentStatistic = value;
                OnPropertyChanged();
            }
        }

        private List<Statistic> Statistics { get; }

        public ResultsWindowViewModel(int correctAnswers, TimeSpan totalTime, TimeSpan fastestAnswerTime, Sum fastestAnswer, TimeSpan slowestAnswerTime, Sum slowestAnswer)
        {
            //string gifFilePrefix = (correctAnswers < 9) ? "Movie_0-9_" : (correctAnswers < 12) ? "Movie_9-12_" : "Movie_12_";

            //if ((StaticCode.Animations != null) && (StaticCode.Animations.Count > 0) && StaticCode.Animations.Any(a => a.StartsWith(gifFilePrefix)))
            //{
            //    MovieGifUrl = String.Format("{0}{1}", StaticCode.AnimationsUrlPrefix, StaticCode.Animations.OrderBy(a => Guid.NewGuid()).First(a => a.StartsWith(gifFilePrefix)));
            //}

            Statistics = new List<Statistic>
            {
                new Statistic {Title = "Score", Details = String.Format("{0} / 12", correctAnswers)},
                new Statistic {Title = "Total Time", Details = String.Format("{0} seconds", new MillisecondsToSecondsStringConverter().Convert(totalTime.TotalMilliseconds, null, null, null))},
                new Statistic {Title = "Average Answer Time", Details = String.Format("{0} seconds", new MillisecondsToSecondsStringConverter().Convert(totalTime.TotalMilliseconds / 12, null, null, null))},
                new Statistic {Title = "Fastest Answer", Details = String.Format("{0} seconds ({1} x {2})", new MillisecondsToSecondsStringConverter().Convert(fastestAnswerTime.TotalMilliseconds, null, null, null), fastestAnswer.Multiplicand, fastestAnswer.Multiplier)},
                new Statistic {Title = "Slowest Answer", Details = String.Format("{0} seconds ({1} x {2})", new MillisecondsToSecondsStringConverter().Convert(slowestAnswerTime.TotalMilliseconds, null, null, null), slowestAnswer.Multiplicand, slowestAnswer.Multiplier)}
            };

            RotateStat();
            var rotateStatTimer = new Timer();
            rotateStatTimer.Elapsed += rotateStatTimer_Elapsed;
            rotateStatTimer.Interval = 5000;
            rotateStatTimer.Start();
        }

        private void rotateStatTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            RotateStat();
        }

        private void RotateStat()
        {
            if ((CurrentStatistic == null) || CurrentStatistic.Equals(Statistics.Last()))
            {
                CurrentStatistic = Statistics.First();
            }
            else
            {
                CurrentStatistic = Statistics.Skip(Statistics.IndexOf(CurrentStatistic) + 1).First();
            }
        }

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}